﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;

using Microsoft.SharePoint.Utilities;

namespace Mystery.SharePoint
{
    /// <summary>
    /// Extension methods for the page
    /// </summary>
    public static class PageExtensionscs
    {
        /// <summary>
        /// Takes the user to the error page
        /// </summary>
        /// <param name="page"></param>
        /// <param name="errorMessage">The error message to show to the user</param>
        public static void ToErrorPage(this Page page, string errorMessage)
        {
            if (page == null) throw new ArgumentNullException("page");

            try
            {
                SPUtility.Redirect("Error.aspx", SPRedirectFlags.RelativeToLayoutsPage, HttpContext.Current,
                    "ErrorText=" + errorMessage);
            }
            catch (System.Threading.ThreadAbortException)
            {
            }
        }
        /// <summary>
        /// Takes the user to the access denied page
        /// </summary>
        /// <param name="page"></param>
        public static void ToAccessDeniedPage(this Page page)
        {
            if (page == null) throw new ArgumentNullException("page");

            try
            {
                SPUtility.Redirect("AccessDenied.aspx", SPRedirectFlags.RelativeToLayoutsPage, HttpContext.Current);
            }
            catch (System.Threading.ThreadAbortException)
            {
            }
        }

        /// <summary>
        /// Takes the user to the site settings page
        /// </summary>
        /// <param name="page"></param>
        public static void ToSettingsPage(this Page page)
        {
            if (page == null) throw new ArgumentNullException("page");

            try
            {
                SPUtility.Redirect("settings.aspx", SPRedirectFlags.RelativeToLayoutsPage, HttpContext.Current);
            }
            catch (System.Threading.ThreadAbortException)
            {
            }
        }
        /// <summary>
        /// Takes the user to a custom page
        /// </summary>
        /// <param name="page"></param>
        /// <param name="url">Url of custom page</param>
        public static void ToCustomPage(this Page page, string url)
        {
            if (page == null) throw new ArgumentNullException("page");
            if (string.IsNullOrEmpty(url) == true) throw new ArgumentNullException("url");

            try
            {
                SPUtility.Redirect(url, SPRedirectFlags.Default, HttpContext.Current);
            }
            catch (System.Threading.ThreadAbortException)
            {
            }
        }
        /// <summary>
        /// Gets a value from the query string, or a default value if it is not present
        /// </summary>
        /// <param name="page"></param>
        /// <param name="key">Name of query string parameter</param>
        /// <param name="defaultValue">Value to use if key is not present</param>
        /// <returns>The value on the querystring or the default value</returns>
        public static string GetRequestQSOrDefault(this Page page, string key, string defaultValue)
        {
            if (page == null) throw new ArgumentNullException("page");
            if (string.IsNullOrEmpty(key) == true) throw new ArgumentNullException("key");

            string str = page.Request.QueryString[key];
            if (str == null)
            {
                return defaultValue;
            }
            return str;
        }
        /// <summary>
        /// Gets a value from the query string, or a default value if it is not present
        /// </summary>
        /// <param name="page"></param>
        /// <param name="key">Name of query string parameter</param>
        /// <param name="defaultValue">Value to use if key is not present</param>
        /// <returns>The value on the querystring or the default value</returns>
        public static int GetRequestQSOrDefault(this Page page, string key, int defaultValue)
        {
            int i;
            if (int.TryParse(page.GetRequestQSOrDefault(key, ""), out i) == true)
            {
                return i;
            }
            return defaultValue;
        }
    }
}
